package wu2020.top.spring.boot.flyway.simple.infrastructure.persistence;

import wu2020.top.spring.boot.flyway.simple.infrastructure.entity.PersonDO;
import wu2020.top.spring.boot.flyway.simple.infrastructure.converter.PersonConverter;
import wu2020.top.spring.boot.flyway.simple.infrastructure.mapper.PersonMapper;
import wu2020.top.spring.boot.flyway.simple.domain.model.person.PersonRepository;
import org.springframework.stereotype.Repository;
import java.util.stream.Collectors;
import com.wu.framework.inner.lazy.database.expand.database.persistence.stream.wrapper.LazyWrappers;
import com.wu.framework.response.Result;
import com.wu.framework.response.ResultFactory;
import jakarta.annotation.Resource;
import wu2020.top.spring.boot.flyway.simple.domain.model.person.Person;
import com.wu.framework.inner.lazy.database.expand.database.persistence.stream.lambda.LazyLambdaStream;
import java.util.List;
import com.wu.framework.inner.lazy.database.expand.database.persistence.domain.LazyPage;
/**
 * describe person 
 *
 * @author Jia wei Wu
 * @date 2024/03/07 09:59 上午
 * @see com.wu.framework.inner.lazy.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence 
 **/
@Repository
public class PersonRepositoryImpl  implements  PersonRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增
     *
     * @param person 新增     
     * @return {@link Result<Person>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Person> story(Person person) {
        PersonDO personDO = PersonConverter.INSTANCE.fromPerson(person);
        lazyLambdaStream.upsert(personDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增
     *
     * @param personList 批量新增     
     * @return {@link Result<List<Person>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<List<Person>> batchStory(List<Person> personList) {
        List<PersonDO> personDOList = personList.stream().map(PersonConverter.INSTANCE::fromPerson).collect(Collectors.toList());
        lazyLambdaStream.upsert(personDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param person 查询单个     
     * @return {@link Result<Person>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Person> findOne(Person person) {
        PersonDO personDO = PersonConverter.INSTANCE.fromPerson(person);
        Person personOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(personDO), Person.class);
        return ResultFactory.successOf(personOne);
    }

    /**
     * describe 查询多个
     *
     * @param person 查询多个     
     * @return {@link Result<List<Person>>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<List<Person>> findList(Person person) {
        PersonDO personDO = PersonConverter.INSTANCE.fromPerson(person);
        List<Person> personList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(personDO), Person.class);
        return ResultFactory.successOf(personList);
    }

    /**
     * describe 分页查询多个
     *
     * @param size 当前页数
     * @param current 当前页
     * @param person 分页查询多个     
     * @return {@link Result<LazyPage<Person>>} 分页领域对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<LazyPage<Person>> findPage(int size,int current,Person person) {
        PersonDO personDO = PersonConverter.INSTANCE.fromPerson(person);
        LazyPage<Person> lazyPage = new LazyPage<>(current,size);
        LazyPage<Person> personLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(personDO),lazyPage, Person.class);
        return ResultFactory.successOf(personLazyPage);
    }

    /**
     * describe 删除
     *
     * @param person 删除     
     * @return {@link Result<Person>}      
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Person> remove(Person person) {
        PersonDO personDO = PersonConverter.INSTANCE.fromPerson(person);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(personDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在
     *
     * @param person 领域对象     
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Boolean> exists(Person person) {
        PersonDO personDO = PersonConverter.INSTANCE.fromPerson(person);
        Boolean exists=lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(personDO));
        return ResultFactory.successOf(exists);
    }

}